package org.jeecg.modules.dataReport.service.impl;

import org.jeecg.modules.dataReport.entity.DataReport;
import org.jeecg.modules.dataReport.entity.DataReportColumn;
import org.jeecg.modules.dataReport.entity.DataReportCalculateRange;
import org.jeecg.modules.dataReport.mapper.DataReportColumnMapper;
import org.jeecg.modules.dataReport.mapper.DataReportCalculateRangeMapper;
import org.jeecg.modules.dataReport.mapper.DataReportMapper;
import org.jeecg.modules.dataReport.service.IDataReportService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 报表管理
 * @Author: jeecg-boot
 * @Date:   2023-02-19
 * @Version: V1.0
 */
@Service
public class DataReportServiceImpl extends ServiceImpl<DataReportMapper, DataReport> implements IDataReportService {

	@Autowired
	private DataReportMapper dataReportMapper;
	@Autowired
	private DataReportColumnMapper dataReportColumnMapper;
	@Autowired
	private DataReportCalculateRangeMapper dataReportCalculateRangeMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(DataReport dataReport, List<DataReportColumn> dataReportColumnList,List<DataReportCalculateRange> dataReportCalculateRangeList) {
		dataReportMapper.insert(dataReport);
		if(dataReportColumnList!=null && dataReportColumnList.size()>0) {
			for(DataReportColumn entity:dataReportColumnList) {
				//外键设置
				entity.setReportId(dataReport.getId());
				dataReportColumnMapper.insert(entity);
			}
		}
		if(dataReportCalculateRangeList!=null && dataReportCalculateRangeList.size()>0) {
			for(DataReportCalculateRange entity:dataReportCalculateRangeList) {
				//外键设置
				entity.setReportId(dataReport.getId());
				dataReportCalculateRangeMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(DataReport dataReport,List<DataReportColumn> dataReportColumnList,List<DataReportCalculateRange> dataReportCalculateRangeList) {
		dataReportMapper.updateById(dataReport);
		
		//1.先删除子表数据
		dataReportColumnMapper.deleteByMainId(dataReport.getId());
		dataReportCalculateRangeMapper.deleteByMainId(dataReport.getId());
		
		//2.子表数据重新插入
		if(dataReportColumnList!=null && dataReportColumnList.size()>0) {
			for(DataReportColumn entity:dataReportColumnList) {
				//外键设置
				entity.setReportId(dataReport.getId());
				entity.setId(null);
				dataReportColumnMapper.insert(entity);
			}
		}
		if(dataReportCalculateRangeList!=null && dataReportCalculateRangeList.size()>0) {
			for(DataReportCalculateRange entity:dataReportCalculateRangeList) {
				//外键设置
				entity.setReportId(dataReport.getId());
				entity.setId(null);
				dataReportCalculateRangeMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		dataReportColumnMapper.deleteByMainId(id);
		dataReportCalculateRangeMapper.deleteByMainId(id);
		dataReportMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			dataReportColumnMapper.deleteByMainId(id.toString());
			dataReportCalculateRangeMapper.deleteByMainId(id.toString());
			dataReportMapper.deleteById(id);
		}
	}
	
}
